//
// Created by Administrator on 2019/10/14.
//

#include "time_util.h"

//当天0时0分0秒时间戳
int get_current_day(time_t t)
{
    struct tm *tm = localtime(&t);
    tm->tm_hour = 0;
    tm->tm_min = 0;
    tm->tm_sec = 0;
    return  mktime(tm);
}

//当前时间戳
char* get_curr_time(void)
{
    char* strtime = (char *)malloc(sizeof(char)*20);
    memset(strtime, 0, sizeof(char)*20);
    time_t now;
    time(&now);
    strftime(strtime, 20, "%Y-%m-%d %H:%M:%S", gmtime(&now));
    return strtime;
}

//当前时间的unix时间戳
int get_curr_unixtime(void)
{
    time_t now;
    int unixtime = time(&now);
    return unixtime;
}

//字符转unix时间戳
int strtotime(char datetime[])
{
    struct tm tm_time;
    int unixtime;
    strptime(datetime, "%Y-%m-%d %H:%M:%S", &tm_time);

    unixtime = mktime(&tm_time);
    return unixtime;
}